/*
 * @author hope.chen, QQ:77132995, email:kazeik@163.com
 * 2021-08-06 11:46
 * 类说明:
 */
import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:gp_print_util/Utils.dart';
import 'package:quiver/strings.dart';

class GpBlueUtils {
  static const platform = const MethodChannel(Utils.receiptPrintMsg);
  static const EventChannel connectionPlatfrom =
      const EventChannel(Utils.MSG_BTCONNECT);

  Future<int> printReceipt(Map<String, dynamic> data) async {
    if (Platform.isAndroid) {
      return await platform
          .invokeMethod("receiptPrint", {"data": jsonEncode(data)});
    }
    return 0;
  }

  connectBt(String address) {
    if (isEmpty(address)) {
      return;
    }
    if (Platform.isAndroid) {
      platform.invokeMethod("connectBT", {"mac": address});
    }
  }

  disconnectBt() {
    if (Platform.isAndroid) {
      platform.invokeMethod("disconnectBT");
    }
  }

  connectStateListener(Function(Map<String, dynamic>)? callback) {
    connectionPlatfrom.receiveBroadcastStream().listen((event) {
      // {code: 1152, address: DC:0D:30:22:66:4B}
      print("value =  $event");
      if (null != callback) callback(event);
    });
  }

  Future<bool> checkBtEnable() async {
    if (Platform.isAndroid) {
      return await platform.invokeMethod("checkBTState");
    } else {
      return false;
    }
  }
}
